
/**
 *关于路由的自定义事件
 */


import store from "@/store";
import router from "@/router";

/**
 *添加菜单权限到router中
 *menu->从后台接口、state、storage中获取的权限列表
 */
export function addLayoutRoutes (menu) {
  var layoutRoute = {
    path: '/layout',
    name: 'layout',
    // component: () => import('@/components/layout/index.vue'),
    component: resolve => require(['@/components/layout/index.vue'], resolve),
    children: []
  }

  // 遍历
  var aChildren = [], homeRoute = null;
  ergodic(menu);

  function ergodic (arr) {
    arr.forEach((item, index) => {
      // 判断是否首页
      if (!homeRoute && item.path == store.state.layout.homePath) {
        homeRoute = item;
      }

      // 添加layout结点
      if (!item.children || !item.children.length) {
        aChildren.push({
          path: item.path,
          name: item.name,
          component: ()=> import(`@/views/${item.component}.vue`),
          meta: {
            title: item.meta.title,
            icon: item.meta.icon,
            keepAlive: item.meta.keepAlive
          }
        })
      } else {
        ergodic(item.children);
      }
    })
  }

  layoutRoute.children = aChildren;

  // 保存路由到缓存
  // 保存首页路由
  store.commit('SET_HOME_ROUTE', homeRoute);
  // 添加权限路由
  router.addRoutes([layoutRoute]);
}


/**
 *判断当前路由 是否有权限访问
 */
export function hasPermission (route) {

}

export default {
  addLayoutRoutes,
  hasPermission,
};





